package com.lemon.smartsafetynew.ui.activity.task;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.entity.safe.company.BindNfcEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.tag.BaseTAG;
import com.lemon.smartsafety.tag.DefaultCode;
import com.lemon.smartsafety.ui.activity.safe.data.BuildingCheckItemBindNfcActivity;
import com.lemon.smartsafety.utils.LogUtils;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.entity.event.task.TaskUpdateEvent;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolBo;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolHistoryItemVo;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolTaskItemVo;
import com.lemon.smartsafetynew.entity.task.InspectionDetailVo;
import com.lemon.smartsafetynew.entity.task.InspectionRecordVo;
import com.lemon.smartsafetynew.entity.task.InspectionVo;
import com.lemon.smartsafetynew.net.services.SafePatrolServices;
import com.lemon.smartsafetynew.ui.activity.MainNewActivity;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafetynew.ui.activity.patrol.SafePatrolCheckItemActivity;
import com.lemon.smartsafetynew.ui.activity.patrol.SafePatrolHistoryDetailActivity;
import com.lemon.smartsafetynew.ui.activity.qrcode.CodeUtils;
import com.lemon.smartsafetynew.ui.activity.qrcode.QRcodeActivity;
import com.lemon.smartsafetynew.ui.adapter.task.ContentAdapter;
import com.lemon.smartsafetynew.ui.adapter.task.RecordAdapter;
import com.lemon.smartsafetynew.util.common.AnimationUtils;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;
import com.lemon.smartsafetynew.util.user.UserTypeUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 巡查详情
 * Created by JiangAn on 2018/5/8.
 */

public class InspectionTaskDetailActivity extends SwipeBackActivity implements RecyclerTClickListener<InspectionRecordVo> {

    @Bind(R.id.tv_item_name)
    TextView tvItemName;
    @Bind(R.id.tv_item_time)
    TextView tvItemTime;
    @Bind(R.id.tv_item_type)
    TextView tvItemType;
    @Bind(R.id.tv_item_people)
    TextView tvItemPeople;
    @Bind(R.id.tv_content)
    TextView tvContent;
    @Bind(R.id.iv_content)
    ImageView ivContent;
    @Bind(R.id.ll_content)
    LinearLayout llContent;
    @Bind(R.id.recyclerViewRecord)
    RecyclerView recyclerViewRecord;
    @Bind(R.id.ll_record)
    LinearLayout llRecord;
    @Bind(R.id.id_flowlayout)
    TagFlowLayout idFlowlayout;
    private InspectionVo mData;
    private RecordAdapter recordAdapter;
    private ContentAdapter contentAdapter;
    private List<InspectionDetailVo.CheckItemsMapBean> listHead = new ArrayList<>();
    private List<InspectionRecordVo> listRecord = new ArrayList<>();
    AlertDialog dialog;
    private String qrCode;
    private String nfcCode;
    InspectionDetailVo vo;
    private boolean isShow = true; //是否需要显示和隐藏
    private Menu mMenu;

    @Override
    protected void getBundleArgs() {
        if (getIntent().getExtras() != null) {
            mData = (InspectionVo) getIntent().getSerializableExtra(KeyConstants.KEY_INSPECTION_TASK_DETAIL);
        }
    }

    @Override
    protected void initTitle() {
        if (mData != null) {
            this.setBarTitle("巡查详情");
        }
    }

    @Override
    protected void initViews() {
        super.initViews();
    }

    @Override
    protected void onResume() {
        super.onResume();
        intData();
    }

    @Override
    protected void initListener() {
        super.initListener();
        ivContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShow) {
                    popupWindow(ivContent);
                }
            }
        });

        idFlowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                if (isShow) {
                    if (!"1".equals(CommonUtil.avoidNullString(listHead.get(position).getIsDelete()))) {
                        qrCode = listHead.get(position).getQrCode();
                        nfcCode = listHead.get(position).getNfcCode();
                        popupWindow(idFlowlayout);
                        return true;
                    }else{
                        Toast.makeText(InspectionTaskDetailActivity.this,"该巡查点已经被删除!",Toast.LENGTH_SHORT).show();
                    }
                }
                return false;
            }
        });
    }

    private void intData() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("taskId", mData.getId());
        SafePatrolServices
                .init()
                .getInspectionListDetail(params);
        LemonRequestParams params1 = new LemonRequestParams("");
        params1.put("taskId", mData.getId());
        SafePatrolServices
                .init()
                .getInspectionListPatrol(params1);
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_inspection_task_detail;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        mMenu = menu;
        getMenuInflater().inflate(R.menu.menu_finish, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        AlertDialog.Builder builder = new AlertDialog.Builder(InspectionTaskDetailActivity.this);
        View view = View
                .inflate(InspectionTaskDetailActivity.this, R.layout.custom_dialog, null);
        builder.setView(view);
        builder.setCancelable(true);
        TextView title = (TextView) view
                .findViewById(R.id.title);//设置标题
        TextView tv_content = (TextView) view
                .findViewById(R.id.tv_content);//输入内容
        Button btn_cancel = (Button) view
                .findViewById(R.id.btn_cancel);//取消按钮
        Button btn_comfirm = (Button) view
                .findViewById(R.id.btn_comfirm);//确定按钮
        dialog = builder.create();
        //确认分两种情况
        btn_comfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vo.getCanFinish()) {
                    LemonRequestParams params = new LemonRequestParams("");
                    params.put("taskId", mData.getId());
                    SafePatrolServices
                            .init()
                            .getFinishInspection(params);
                }
                dialog.dismiss();
            }
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        if (item.getItemId() == R.id.action_task_finish) {
            if (vo.getCanFinish()) {
                btn_cancel.setVisibility(View.VISIBLE);
                tv_content.setText("是否确认结束该任务?");
                title.setText("任务完成");
            } else {
                btn_cancel.setVisibility(View.GONE);
                tv_content.setText("有巡查点未完成巡查，不能结束该任务!");
                title.setText("任务未完成");
            }
            dialog.show();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(InspectionRecordVo recordVo, int position) {
        //点击进入历史记录
        Bundle bundle = new Bundle();
        SafePatrolHistoryItemVo itemVo = new SafePatrolHistoryItemVo();
        itemVo.setCheck_id(CommonUtil.avoidNullString(recordVo.getCheck_id()));
        itemVo.setItem_id(CommonUtil.avoidNullString(recordVo.getItem_id()));
        bundle.putSerializable(KeyConstants.KEY_SAFE_PATROL_CHECK_ITEM_HISTORY_RESULT, itemVo);
        UIHelper.showActivity(this, bundle, SafePatrolHistoryDetailActivity.class);
    }


    //============= 二维码和NFC ==============
    private PopupWindow popupWindow;
    private static final int DURATION_TIME = 300;
    private static final int DURATION_INTERVAL = 100;
    private static final int FIRST_ROW = 100;
    private static final float[] START_ROTATION = new float[]{0, 45};
    private static final float[] CLOSE_ROTATION = new float[]{45, 0};
    private float[] START_TRANSLATION_Y = null;

    private void popupWindow(View parent) {
        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(InspectionTaskDetailActivity.this).inflate(
                R.layout.layout_main_plus, null);

        popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);

        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });

        // 设置好参数之后再show
        popupWindow.showAtLocation(parent, Gravity.NO_GRAVITY, 0, 0);


        // ============= Listener =========
        final ImageView closeImageView = (ImageView) contentView.findViewById(R.id.close_image);
        final LinearLayout nfcLayout = (LinearLayout) contentView.findViewById(R.id.nfc_layout);
        final LinearLayout qrLayout = (LinearLayout) contentView.findViewById(R.id.qrcode_layout);

        float height = CommonUtil.convertDipOrPx(InspectionTaskDetailActivity.this, FIRST_ROW);
        START_TRANSLATION_Y = new float[]{height, -50, -25, -10, 0, -20, -10, 0};

        closeImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AnimationUtils.closeRotationAnimation(closeImageView, DURATION_TIME, CLOSE_ROTATION);
                AnimationUtils.closeAnimation(nfcLayout, DURATION_TIME, CommonUtil.convertDipOrPx(InspectionTaskDetailActivity.this, FIRST_ROW));
                AnimationUtils.closeAnimation(qrLayout, DURATION_TIME, CommonUtil.convertDipOrPx(InspectionTaskDetailActivity.this, FIRST_ROW));

                closeImageView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        popupWindow.dismiss();
                    }
                }, DURATION_TIME);

            }
        });

        nfcLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startNfc();
                //mHUDUtil.showInfoTip("暂未开放");
                popupWindow.dismiss();
            }
        });

        qrLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startQrcode();
                popupWindow.dismiss();
            }
        });

        // =========== 动画 ============
        AnimationUtils.startRotationAnimation(closeImageView, DURATION_TIME, START_ROTATION);
        AnimationUtils.startAnimation(nfcLayout, DURATION_TIME + DURATION_INTERVAL, START_TRANSLATION_Y);
        AnimationUtils.startAnimation(qrLayout, DURATION_TIME + 2 * DURATION_INTERVAL, START_TRANSLATION_Y);
    }

    /**
     * 跳转NFC 读取页面
     */
    private void startNfc() {
        Bundle bundle = new Bundle();
        bundle.putString(BaseTAG.COMMON_BASE_TAG, InspectionTaskDetailActivity.class.getName());
        UIHelper.showActivity(this, bundle, BuildingCheckItemBindNfcActivity.class);
    }


    /**
     * 跳转QR code 读取页面
     */
    private void startQrcode() {
        try {
            Intent intent = new Intent(this, QRcodeActivity.class);
            startActivityForResult(intent, DefaultCode.REQUEST_CODE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * QR code读取结果
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        /**
         * 处理二维码扫描结果
         */
        if (requestCode == DefaultCode.REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {

                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }

                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    String company_id = "";
                    if (AccountUtil.getCompanyId() == null) {
                        company_id = "";
                    } else {
                        company_id = AccountUtil.getCompanyId();
                    }
                    if (mData.getTaskType().equals("1")) {
                        if (!qrCode.equals(result)) {
                            //扫描地址不匹配
                            Toast.makeText(InspectionTaskDetailActivity.this, "非该巡查点对应的二维码", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }

                    LemonRequestParams params = new LemonRequestParams("");
                    params.put("company_id", company_id);
                    params.put("qr_code", result);
                    params.put("nfc_code", "");

                    SafePatrolServices
                            .init()
                            .getSafePatrolInspectionItem(params);

                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(InspectionTaskDetailActivity.this, "解析二维码失败", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainThread(HttpCallbackEvent event) {
        if (KeyConstants.URL_GET_INSPECTION_TASK_DETAIL.equals(event.getTag())) {                             //获取巡查任务
            if (event.getHttpFlow() == HandlerCallback.HttpFlow.START) {
                mHUDUtil.showHUD();
            } else if (event.getHttpFlow() == HandlerCallback.HttpFlow.FINISH) {
                mHUDUtil.hidden();
                if (event.getEventResult() == Constants.Result.SUCCESS) {
                    if (event.getObj() instanceof InspectionDetailVo) {
                        vo = (InspectionDetailVo) event.getObj();
                        tvItemName.setText(CommonUtil.avoidNullString(vo.getTaskName()));
                        tvItemTime.setText(CommonUtil.avoidNullString(vo.getStartDateStr())
                                + "\n~ " + CommonUtil.avoidNullString(vo.getEndDateStr()));
                        tvItemPeople.setText(CommonUtil.avoidNullString(vo.getTaskUserNames()));
                        if (!vo.getDisable()) {
                            isShow = true;
                            if (mMenu != null) {
                                mMenu.getItem(0).setVisible(true);
                            }
                            ivContent.setVisibility(View.VISIBLE);
                        } else {
                            isShow = false;
                            if (mMenu != null) {
                                mMenu.getItem(0).setVisible(false);
                            }
                            ivContent.setVisibility(View.INVISIBLE);
                        }
                        if (vo.getTaskType().equals("1")) {
                            tvItemType.setText("固定任务");
                            idFlowlayout.setVisibility(View.VISIBLE);
                            llContent.setVisibility(View.GONE);
                            if (vo.getCheckItemsMap() != null && vo.getCheckItemsMap().size() > 0) {
                                listHead.clear();
                                listHead.addAll(vo.getCheckItemsMap());
                                if (contentAdapter == null) {
                                    contentAdapter = new ContentAdapter(listHead, InspectionTaskDetailActivity.this);
                                    contentAdapter.setDisable(isShow);
                                    idFlowlayout.setAdapter(contentAdapter);
                                } else {
                                    contentAdapter.setDisable(isShow);
                                    contentAdapter.notifyDataChanged();
                                }
                            }
                        } else {
                            llContent.setVisibility(View.VISIBLE);
                            tvItemType.setText("随机任务");
                            tvContent.setText(vo.getTaskContent());
                        }
                    }
                }
            }
        } else if (KeyConstants.URL_GET_INSPECTION_SAFE_PATROL_ITEM.equals(event.getTag())) {                  //获取检查项
            if (event.getHttpFlow() == HandlerCallback.HttpFlow.START) {
                mHUDUtil.showHUD();
            } else if (event.getHttpFlow() == HandlerCallback.HttpFlow.FINISH) {
                mHUDUtil.hidden();
                if (event.getEventResult() == Constants.Result.SUCCESS) {
                    if (event.getObj() instanceof SafePatrolTaskItemVo) {
                        SafePatrolTaskItemVo itemVo = (SafePatrolTaskItemVo) event.getObj();
                        if (SafePatrolBo.ItemType.self_check_point.getValue().equals(itemVo.getItem_type())) {
                            /**
                             * 企业风险点
                             */
                            if (UserTypeUtil.isGovType(AccountUtil.getUserType())) {
                                Toast.makeText(InspectionTaskDetailActivity.this, "企业风险点，您无法检查", Toast.LENGTH_SHORT).show();
                            } else {
                                if (AccountUtil.getCompanyId().equals(itemVo.getCompany_id())) {
                                    /**
                                     * 跳转检查项详细的检查页面
                                     */
                                    Bundle bundle = new Bundle();
                                    bundle.putSerializable(KeyConstants.KEY_SAFE_PATROL_CHECK_ITEM_TASK, (SafePatrolTaskItemVo) event.getObj());
                                    bundle.putInt(KeyConstants.KEY_SAFE_PATROL_TASK_ID, 1);// task_id ：没有任何作用的   只是没时间修改原先的逻辑了，这里只是为了保持智慧安全云一样的页面传输结构，减少修改
                                    bundle.putString(KeyConstants.KEY_SAFE_PATROL_TASK_ID_NEW, "");// taskId 根据任务iD进行巡检
                                    bundle.putString(KeyConstants.KEY_SAFE_PATROL_TASK_ID_NEW, mData.getId());// taskId 根据任务iD进行巡检
                                    UIHelper.showActivity(InspectionTaskDetailActivity.this, bundle, SafePatrolCheckItemActivity.class);

                                } else {
                                    Toast.makeText(InspectionTaskDetailActivity.this, "非本企业的风险点无法检查", Toast.LENGTH_SHORT).show();
                                }
                            }
                        } else if (SafePatrolBo.ItemType.grid_check_point.getValue().equals(itemVo.getItem_type())) {
                            /**
                             * 政府网格点
                             */
                            if (UserTypeUtil.isGovType(AccountUtil.getUserType())) {
                                /**
                                 * 跳转检查项详细的检查页面
                                 */
                                Bundle bundle = new Bundle();
                                bundle.putSerializable(KeyConstants.KEY_SAFE_PATROL_CHECK_ITEM_TASK, (SafePatrolTaskItemVo) event.getObj());
                                bundle.putInt(KeyConstants.KEY_SAFE_PATROL_TASK_ID, 1);// task_id ：没有任何作用的   只是没时间修改原先的逻辑了，这里只是为了保持智慧安全云一样的页面传输结构，减少修改
                                bundle.putString(KeyConstants.KEY_SAFE_PATROL_TASK_ID_NEW, "");// taskId 根据任务iD进行巡检
                                bundle.putString(KeyConstants.KEY_SAFE_PATROL_TASK_ID_NEW, mData.getId());// taskId 根据任务iD进行巡检
                                UIHelper.showActivity(InspectionTaskDetailActivity.this, bundle, SafePatrolCheckItemActivity.class);
                            } else {
                                Toast.makeText(InspectionTaskDetailActivity.this, "网格检查点，您无法检查", Toast.LENGTH_SHORT).show();
                            }
                        }

                    } else {
                        Toast.makeText(InspectionTaskDetailActivity.this, "未知错误", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(InspectionTaskDetailActivity.this, event.getMsg(), Toast.LENGTH_SHORT).show();
                }
            }
        } else if (KeyConstants.URL_GET_INSPECTION_TASK_PARTROL_RECORD.equals(event.getTag())) {
            if (event.getHttpFlow() == HandlerCallback.HttpFlow.START) {
            } else if (event.getHttpFlow() == HandlerCallback.HttpFlow.FINISH) {
                if (event.getEventResult() == Constants.Result.SUCCESS) {
                    listRecord.clear();
                    listRecord.addAll((List<InspectionRecordVo>) event.getObj());
                    if (listRecord != null && listRecord.size() > 0) {
                        llRecord.setVisibility(View.GONE);
                        recyclerViewRecord.setVisibility(View.VISIBLE);
                        if (recordAdapter == null) {
                            recordAdapter = new RecordAdapter(listRecord, this);
                            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
                            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                            recyclerViewRecord.setLayoutManager(linearLayoutManager);
                            recyclerViewRecord.setAdapter(recordAdapter);
                        } else {
                            recordAdapter.notifyDataSetChanged();
                        }
                    } else {
                        llRecord.setVisibility(View.VISIBLE);
                        recyclerViewRecord.setVisibility(View.GONE);
                    }
                }
            }
        } else if (KeyConstants.URL_GET_FINISH_INSPECTION_TASK.equals(event.getTag())) {
            if (event.getHttpFlow() == HandlerCallback.HttpFlow.START) {
                mHUDUtil.showHUD();
            } else if (event.getHttpFlow() == HandlerCallback.HttpFlow.FINISH) {
                mHUDUtil.hidden();
                if (event.getEventResult() == Constants.Result.SUCCESS) {
                    AppManager.getAppManager().finishActivity(InspectionTaskDetailActivity.this);
                } else {
                    mHUDUtil.showErrorTip(event.getMsg());
                }
            }
        }
    }

    /**
     * NFC 读取结果
     */
    @Subscribe
    public void onEventMainThread(final BindNfcEvent event) {
        if (MainNewActivity.class.getName().equals(event.getTag())) {
            if (!com.lemon.smartsafety.utils.CommonUtil.strIsEmpty(event.getCode())) {
                LogUtils.LOGE("巡查NFC:" + event.getCode());

                //TODO 请求NFC
                String company_id = "";
                if (AccountUtil.getCompanyId() == null) {
                    company_id = "";
                } else {
                    company_id = AccountUtil.getCompanyId();
                }
                if (mData.getTaskType().equals("1")) {
                    if (!nfcCode.equals(event.getCode())) {
                        //扫描地址不匹配
                        Toast.makeText(InspectionTaskDetailActivity.this, "非该巡查点对应的二维码", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }

                LemonRequestParams params = new LemonRequestParams("");
                params.put("company_id", company_id);
                params.put("qr_code", "");
                params.put("nfc_code", event.getCode());

                SafePatrolServices
                        .init()
                        .getSafePatrolInspectionItem(params);
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppService.getBus().post(new TaskUpdateEvent());
    }
}
